package com.manfred.axis2.test.service;

import javax.xml.stream.XMLStreamException;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.commons.logging.LogFactory;

public class CMSService {

	public OMElement getNumberOfArticles(OMElement element)
			throws XMLStreamException {

		element.build();
		element.detach();
//		String rootName = element.getLocalName();
		OMElement categoryElement = element.getFirstElement();
//		String categoryElementName = categoryElement.getLocalName();
		String categoryValue = categoryElement.getText();

		SOAPFactory factory = OMAbstractFactory.getSOAP12Factory();
		OMNamespace namespace = factory.createOMNamespace(
				"http://daily-moon.com/cms/", "resp");
		OMElement resultElem = factory.createOMElement("numberOfArticles",
				namespace);

		String actualValue = (articleCount(categoryValue)).toString();
		resultElem.setText(actualValue);

		return resultElem;
	}

	private Integer articleCount(String catId) {

		// Perform some function such as searching the CMS
		// database, and return the actual value. For our
		// purposes, you'll hardcode it.
		return new Integer(42);

	}

	public void addArticle(OMElement element) throws XMLStreamException {
		element.build();
		System.out.println("System.out.println: "+element);
	}

}